<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/11/25
 * Time: 11:04
 */

namespace Admin\Model;
use Think\Model;

class CourseChapterModel extends Model
{
    protected $trueTableName='t_course_chapter';

    public function insertChapters($courseChapteData){
        $result = $this->add($courseChapteData);
        return $result;
    }
    public function saveChapters($courseChapteData){
        $result = $this->save($courseChapteData);
        return $result;
    }
    /**
     * 通过id查找章节信息
     *
     */
    public function getinfo($id) {
        $data['course_id'] = $id;
        $result = $this->where($data)->select();
        if($result) {
            $num=0;
            foreach($result as &$user) {
                if(!empty($user['imgs'])){
                    $imgs =array();
                    $user['imgs'] = json_decode($user['imgs'],true);
                    foreach($user['imgs'] as $k=>$v){
//                        $imgs[$k]['path']=$v->path;
//                        $imgs[$k]['name']=$v->name;
//                        $imgs[$k]['size']=$v->size;
                        $imgs[$k]['path']=$v['path'];
                        $imgs[$k]['name']=$v['name'];
                        $imgs[$k]['size']=$v['size'];

                    }
                    $user['imgs'] =$imgs;
                }else{
                    $user['imgs'] = '';
                }
                if(!empty($user['attach'])){
                    $attch = '';
                    $user['attach'] = json_decode($user['attach'],true);
                    foreach($user['attach'] as $v){
                        $attch.=$v['name'].'(*)'.$v['url'].'(#)';
                    }
                    $user['attach']=$attch;
                }else{
                    $user['attach'] = '';
                }
                $num+=1;
                $user['num']=$num;
            }
        }
        return $result;
    }
    /**
     * 通过id查找课程辅助资料信息
     *
     */
    public function getAttinfo($id) {
        $data['course_id'] = $id;
        $result = $this->where($data)->field('id,course_id,attach')->select();
        if($result) {
            $num=0;
            foreach($result as &$user) {
                if(!empty($user['attach'])){
                    $attachs =array();
                    $user['attach'] = json_decode($user['attach'],true);
                    foreach($user['attach'] as $k=>$v){
                        $attachs[$k]['url']=$v['url'];
                        $attachs[$k]['name']=$v['name'];


                    }
                    $user['attach'] =$attachs;
                }else{
                    $user['attach'] = '';
                }

                $num+=1;
                $user['num']=$num;
            }
        }
        return $result;
    }



}
